using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using System.Diagnostics;

namespace BizTalk.BuildGenerator.Tasks.BAM
{
    /// <summary>
    /// This is a base class for tasks which will use BM.exe
    /// </summary>
    public class BaseBMTask : Task
    {
        private string _BMPath = @"c:\Program Files\Microsoft BizTalk Server 2009\Tracking\BM";
        private string _Server;
        private string _Database;

        /// <summary>
        /// The name of the database to pass to BM.exe, this is optional
        /// </summary>
        public string Database
        {
            get { return _Database; }
            set { _Database = value; }
        }
        /// <summary>
        /// The name of the database server to pass to BM.exe, this is optional
        /// </summary>
        public string Server
        {
            get { return _Server; }
            set { _Server = value; }
        }
        /// <summary>
        /// The path to BM.exe, this is optional
        /// </summary>
        public string BMPath
        {
            get { return _BMPath; }
            set { _BMPath = value; }
        }
        /// <summary>
        /// Executes BM.exe with a set of arguments
        /// </summary>
        /// <param name="arguments"></param>
        protected bool ExecuteBM(string arguments)
        {
            Microsoft.Build.Tasks.Exec exec = new Microsoft.Build.Tasks.Exec();
            exec.BuildEngine = this.BuildEngine;
            exec.Command = "\"" + this._BMPath + "\" " + arguments;

            if (!string.IsNullOrEmpty(this._Database))
                exec.Command += string.Format(" -Database:{0}", this._Database);
            if(!string.IsNullOrEmpty(this._Server))
                exec.Command += string.Format(" -Server:{0}", this._Server);

            Trace.WriteLine("Executing command: " + exec.Command);
            return exec.Execute();            
        }
        /// <summary>
        /// Execute method
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
